import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 并发管理器/请求定义（并发程序）
 * @date: 2018-9-10


 */
import { getResponse, createPagination } from 'utils/utils';
import { queryMapIdpValue } from 'cpaas-front/lib/services/api';
import { fetchConcurrentList as _fetchConcurrentList, createConcurrent as _createConcurrent, updateConcurrent as _updateConcurrent, fetchConcurrentDetail as _fetchConcurrentDetail, deleteLine as _deleteLine, fetchAssignedPermission as _fetchAssignedPermission, createPermission as _createPermission, updatePermission as _updatePermission } from "../services/concurrentService";
export default {
  namespace: 'concurrent',
  state: {
    list: [],
    // 数据列表
    pagination: {},
    // 分页器
    concurrentDetail: {},
    // 详情数据
    line: [],
    // 条件行
    paramFormatList: [],
    // 参数格式
    editTypeList: [] // 编辑类型

  },
  effects: {
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_, _ref) {
      var call, put, res, paramFormatList, editTypeList;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.t0 = getResponse;
              _context.next = 4;
              return call(queryMapIdpValue, {
                paramFormatList: 'CSDR.PARAM_FORMAT',
                editTypeList: 'CSDR.PARAM_EDIT_TYPE'
              });

            case 4:
              _context.t1 = _context.sent;
              res = (0, _context.t0)(_context.t1);
              paramFormatList = res.paramFormatList, editTypeList = res.editTypeList;

              if (!res) {
                _context.next = 10;
                break;
              }

              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  paramFormatList: paramFormatList,
                  editTypeList: editTypeList
                }
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    fetchConcurrentList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchConcurrentList(_ref2, _ref3) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchConcurrentList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(_fetchConcurrentList, payload);

            case 4:
              result = _context2.sent;
              result = getResponse(result);

              if (!result) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  list: result.content,
                  pagination: createPagination(result)
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchConcurrentList);
    }),
    // 创建
    createConcurrent: /*#__PURE__*/_regeneratorRuntime.mark(function createConcurrent(_ref4, _ref5) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createConcurrent$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call;
              _context3.next = 4;
              return call(_createConcurrent, payload);

            case 4:
              res = _context3.sent;
              return _context3.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, createConcurrent);
    }),
    // 更新
    updateConcurrent: /*#__PURE__*/_regeneratorRuntime.mark(function updateConcurrent(_ref6, _ref7) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateConcurrent$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_updateConcurrent, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, updateConcurrent);
    }),
    // 查询job详情
    fetchConcurrentDetail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchConcurrentDetail(_ref8, _ref9) {
      var payload, call, put, res, result;
      return _regeneratorRuntime.wrap(function fetchConcurrentDetail$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call, put = _ref9.put;
              _context5.next = 4;
              return call(_fetchConcurrentDetail, payload);

            case 4:
              res = _context5.sent;
              result = getResponse(res);

              if (!result) {
                _context5.next = 9;
                break;
              }

              _context5.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  concurrentDetail: result
                }
              });

            case 9:
              return _context5.abrupt("return", result);

            case 10:
            case "end":
              return _context5.stop();
          }
        }
      }, fetchConcurrentDetail);
    }),
    // 删除行
    deleteLine: /*#__PURE__*/_regeneratorRuntime.mark(function deleteLine(_ref10, _ref11) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deleteLine$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call;
              _context6.next = 4;
              return call(_deleteLine, _objectSpread({}, payload));

            case 4:
              result = _context6.sent;
              return _context6.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, deleteLine);
    }),
    // 获取已分配的权限
    fetchAssignedPermission: /*#__PURE__*/_regeneratorRuntime.mark(function fetchAssignedPermission(_ref12, _ref13) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchAssignedPermission$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call, put = _ref13.put;
              _context7.next = 4;
              return call(_fetchAssignedPermission, payload);

            case 4:
              result = _context7.sent;
              result = getResponse(result);

              if (!result) {
                _context7.next = 9;
                break;
              }

              _context7.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  permissionsList: result.content,
                  permissionsPagination: createPagination(result)
                }
              });

            case 9:
              return _context7.abrupt("return", result);

            case 10:
            case "end":
              return _context7.stop();
          }
        }
      }, fetchAssignedPermission);
    }),
    // 创建权限
    createPermission: /*#__PURE__*/_regeneratorRuntime.mark(function createPermission(_ref14, _ref15) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createPermission$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref14.payload;
              call = _ref15.call;
              _context8.next = 4;
              return call(_createPermission, payload);

            case 4:
              res = _context8.sent;
              return _context8.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context8.stop();
          }
        }
      }, createPermission);
    }),
    // 更新权限
    updatePermission: /*#__PURE__*/_regeneratorRuntime.mark(function updatePermission(_ref16, _ref17) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updatePermission$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref16.payload;
              call = _ref17.call;
              _context9.next = 4;
              return call(_updatePermission, payload);

            case 4:
              res = _context9.sent;
              return _context9.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, updatePermission);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref18) {
      var payload = _ref18.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};